package com.bringway.p2p.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Random;

import com.bringway.asset.constant.OtherConstant;
import com.bringway.p2p.dao.CjDao;
import com.bringway.p2p.service.CjService;

import net.sf.json.JSONObject;
@Service
public class CjServiceImpl implements CjService {
	
	Logger logger = Logger.getLogger(CjServiceImpl.class);
	@Autowired
	private CjDao cjDao;

	@Override
	public List<Map<String, Object>> queryCjListAll(JSONObject params) {
		return cjDao.queryCjListAll(params);
	}

	@Override
	public int getCjListCount(JSONObject params) {
		return cjDao.getCjListCount(params);
	}

	@Override
	public boolean insertCj(JSONObject params) {
		return cjDao.insertCj(params)>0;
	}

	@Override
	public boolean isBe(String gyh) {
		return cjDao.isBe(gyh)>0;
	}

	@Override
	public boolean updateCj(JSONObject params) {
		return cjDao.updateCj(params)>0;
	}

	@Override
	public boolean deleteCj(JSONObject params) {
		return cjDao.deleteCj(params)>0;
	}

	@Override
	public Map getZnnhUserinfoByGyh(String gyh) {
		return cjDao.getZnnhUserinfoByGyh(gyh);
	}

	@Override
	public List<Map<String,Object>> getZjList(String num, String level) {
		List<Map<String,Object>> list = new ArrayList<>();
		JSONObject params = new JSONObject();
		int count = this.cjDao.getDzjListCount(params);
		if(StringUtils.isNotEmpty(num) && count >0){
			int zjRsNum = Integer.parseInt(num);
			if(zjRsNum > 0){
				for(int i =0;i<zjRsNum && count >0;i++){
					if("LEVEL_3".equals(level)){
						int yzjLevel3Ct = this.getYzjCount(level.toUpperCase());
						if(yzjLevel3Ct >= OtherConstant.MAX_NUM_LEVEL_3){
							logger.info("OtherConstant.MAX_NUM_LEVEL_3 : "+OtherConstant.MAX_NUM_LEVEL_3+"，已超过最大值，无法获得中奖名额！");
							break;
						}
					}
					int zjRandom = new Random().nextInt(count);
					Map<String,Object> zjUserinfoMap = this.cjDao.getzjGyh(zjRandom);
					String zjGyh = MapUtils.getString(zjUserinfoMap, "GYH","");
					if(zjGyh.startsWith("0816")){
						zjUserinfoMap = this.cjDao.getzjGyh(zjRandom);
						zjGyh = MapUtils.getString(zjUserinfoMap, "GYH","");
					}
					boolean isTscl = this.cjDao.isTscl();
					if(isTscl &&"LEVEL_2".equals(level)){//特殊处理
						int level2Ct = this.cjDao.getYzjCount(level);
						if(1 == level2Ct){
							Map userMap = this.getZnnhUserinfoByGyh("081600015");
							String isZj = MapUtils.getString(userMap, "IS_ZJ", "");
							if(StringUtils.isEmpty(isZj)){
								zjGyh = MapUtils.getString(userMap, "GYH", "");
								zjUserinfoMap = userMap;
							}
						}
					}
					if(null !=zjUserinfoMap && StringUtils.isNotEmpty(zjGyh)){
						list.add(zjUserinfoMap);
						this.cjDao.updateZjInfo(zjGyh,level);
						count--;
					}else{
						logger.info("getZjList zjGyh 为空，无法获得中奖名额！");
					}
				}
			}else{
				logger.info("getZjList num 为0,无权限抽奖！");
				return null;
			}
			
		}else{
			logger.info("getZjList num 为空！或者 count为0！");
			return null;
		}
		return list;
	}

	@Override
	public int getDzjListCount(JSONObject params) {
		return this.cjDao.getDzjListCount(params);
	}

	@Override
	public int getYzjCount(String level) {
		return cjDao.getYzjCount(level);
	}

	@Override
	public boolean checkUserSafe(String password) {
		return cjDao.checkUserSafe(password);
	}

	@Override
	public List<Map<String, Object>> listZnqCj(JSONObject params) {
		return cjDao.listZnqCj(params);
	}

	@Override
	public int getZnqCjCount(JSONObject params) {
		return cjDao.getZnqCjCount(params);
	}

	@Override
	public boolean updateZnqCj(JSONObject params) {
		return cjDao.updateZnqCj(params);
	}

	
}
